package com.art.sunflower.service.impl;

import com.art.sunflower.exception.NullNewsException;
import com.art.sunflower.mapper.NewsMapper;
import com.art.sunflower.pojo.News;
import com.art.sunflower.service.NewsService;
import com.art.sunflower.util.MyPageInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class NewsServiceImpl implements NewsService {

    @Resource
    private NewsMapper newsMapper;

    @Override
    public int addNews(News news) {
        return newsMapper.addNews(news);
    }

    @Override
    public int deleteById(int id) {
        return newsMapper.deleteById(id);
    }

    @Override
    public int updateNewsById(News news) {
        return newsMapper.updateNewsById(news);
    }

    @Override
    public Map<String, String> selectById(int id) {
        return newsMapper.selectById(id);
    }

    @Override
    public MyPageInfo findAllNews(Integer pageSize, Integer pageNo) throws NullNewsException {
        PageHelper.startPage(pageNo, pageSize);
        List<Map<String, Object>> news = newsMapper.findAllNews();
        if (news.size() == 0)
            throw new NullNewsException();
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(news);
        return new MyPageInfo(pageInfo);
    }

}
